Import Libraries


In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as scp

Read Image


In [2]:
bike = cv2.imread('bike.jpg', cv2.IMREAD_GRAYSCALE)
img = bike[250:550,400:650]

Shifting Right - Impulse Filter (left symmetric)


In [3]:
Right_shift = np.zeros((150,150))
Right_shift[74,0] = 1 # 1 on left column and the middle row of array and rest all zero
filtered_Right_shift = scp.correlate(img, Right_shift)

In [4]:
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Original')


Out[4]:
(<matplotlib.axes._subplots.AxesSubplot at 0x7f6c4ccbacc0>,
 <matplotlib.image.AxesImage at 0x7f6c4a3bc240>,
 <matplotlib.text.Text at 0x7f6c4a396828>)

In [5]:
plt.subplot(1,2,2), plt.imshow(filtered_Right_shift, cmap='gray'), plt.title('Right Shifted')


Out[5]:
(<matplotlib.axes._subplots.AxesSubplot at 0x7f6c4ccbaa20>,
 <matplotlib.image.AxesImage at 0x7f6c4a340828>,
 <matplotlib.text.Text at 0x7f6c4a30be10>)

In [6]:
plt.show()


Shifting Left - Impulse Filter (Right symmetric)


In [7]:
Left_shift = np.zeros((150,150))
Left_shift[74,149] = 1 # 1 on bottom row and the middle column of array and rest all zero
filtered_Left_shift = scp.correlate(img, Left_shift)
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1,2,2), plt.imshow(filtered_Left_shift, cmap='gray'), plt.title('Left Shifted')
plt.show()


Shifting Top - Impulse Filter (Bottom symmetric)


In [8]:
Top_shift = np.zeros((150,150))
Top_shift[149,74] = 1 # 1 on bottom row and the middle column of array and rest all zero
filtered_Top_shift = scp.correlate(img, Top_shift)
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1,2,2), plt.imshow(filtered_Top_shift, cmap='gray'), plt.title('Top Shifted')
plt.show()


Shifting Bottom - Impulse Filter (Top symmetric)


In [9]:
Bottom_shift = np.zeros((150,150))
Bottom_shift[0,74] = 1 # 1 on bottom row and the middle column of array and rest all zero
filtered_Bottom_shift = scp.correlate(img, Bottom_shift)
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1,2,2), plt.imshow(filtered_Bottom_shift, cmap='gray'), plt.title('Bottom Shifted')
plt.show()